/*
 * @Author: yang bo
 * @Date: 2020-12-17 22:40:21
 * @LastEditors: yang bo
 * @LastEditTime: 2020-12-21 13:57:46
 * @Description:
 */
import { alertTwoButt, platform, settingPrivacy } from "../base";

/**
 * 扫描身份证
 * @param {*} params
 */
export function idCardOCR() {
	return new Promise(function (resolve, reject) {
		window.YHAuthorize.callHandler("OCR_IDCardScan", {}, resp => {
			if (resp.status == "1000") {
				resolve(resp);
			} else {
				reject(resp);
				if (resp.status == "3001") {
					// 权限相关
					let showMsg = "";
					if (platform() == "ios") {
						showMsg = `本应用暂时无法访问相机，请进入“设置-隐私-相机”进行授权。`;
					} else {
						showMsg = `暂时无法打开相机，请进入权限管理中授信本应用读取存储文件或照片。`;
					}
					alertTwoButt(showMsg, "去设置", "取消", () => {
						settingPrivacy();
					});
				}
			}
		});
	});
}

/**
 * 扫描银行卡
 * @param {*} params
 */
export function bankCardOCR() {}

/**
 * 活体检测
 * @param {*} params
 */
export function liveFace(params) {
	return new Promise(function (resolve, reject) {
		console.log("--活体检测");
		window.YHAuthorize.callHandler("Page_LiveFace", params, resp => {
			console.log("人脸参数：", params);
			console.log("人脸结果：", resp);
			let error_message = {
				LIVENESS_FINISH: "活体完成",
				BIZ_TOKEN_DENIED: "传入的 biz_token 不符合要求",
				"ILLEGAL_PARAMETER:{livenesstype}": "传入参数不合法livenesstype",
				"ILLEGAL_PARAMETER:{context}": "传入参数不合法context",
				"ILLEGAL_PARAMETER:{token}": "传入参数不合法token",
				"ILLEGAL_PARAMETER:{host}": "传入参数不合法host",
				"ILLEGAL_PARAMETER:{missing_liveness_config}":
					"传入参数不合法missing_liveness_config",
				"ILLEGAL_PARAMETER:{response_result_is_null}":
					"传入参数不合法response_result_is_null",
				"ILLEGAL_PARAMETER:{jsonexception_200}":
					"传入参数不合法jsonexception_200",
				"ILLEGAL_PARAMETER:{request_data_is_null}":
					"传入参数不合法request_data_is_null",
				"ILLEGAL_PARAMETER:{request_data_error}":
					"传入参数不合法request_data_error",
				"ILLEGAL_PARAMETER:{jsonexception_400}":
					"传入参数不合法jsonexception_400",
				"AUTHENTICATION_FAIL:{illegal_param}": "鉴权失败illegal_param",
				"AUTHENTICATION_FAIL:{illegal_handle}": "鉴权失败illegal_handle",
				"AUTHENTICATION_FAIL:{illegal_index}": "鉴权失败illegal_index",
				"AUTHENTICATION_FAIL:{expire}": "鉴权失败expire",
				"AUTHENTICATION_FAIL:{bundleid_error}": "鉴权失败bundleid_error",
				"AUTHENTICATION_FAIL:{license_error}": "鉴权失败license_error",
				"AUTHENTICATION_FAIL:{model_error}": "鉴权失败model_error",
				"AUTHENTICATION_FAIL:{algo_error}": "鉴权失败algo_error",
				MOBILE_PHONE_NOT_SUPPORT: "手机在不支持列表里",
				REQUEST_FREQUENTLY: "同一台设备同时存在多次调用",
				NETWORK_TIME_OUT: "网络请求超时",
				INTERNAL_ERROR: "网络配置错误",
				INVALID_BUNDLE_ID: "信息验证失败，请重启程序或设备后重试",
				NETWORK_ERROR: "连不上互联网，请连接上互联网后重试",
				USER_CANCEL: "用户取消",
				NO_CAMERA_PERMISSION: "没有打开相机的权限，请开启权限后重试",
				DEVICE_NOT_SUPPORT: "无法启动相机，请确认摄像头功能完好",
				FACE_INIT_FAIL: "无法启动人脸识别，请稍后重试",
				NO_WRITE_EXTERNAL_STORAGE_PERMISSION:
					"无法读取写SD卡的权限，请开启权限后重试",
				LIVENESS_FAILURE: "活体失败",
				GO_TO_BACKGROUND: "应用退到后台，活体检测失败。",
				LIVENESS_TIME_OUT: "操作超时，由于用户在长时间没有进行操作"
			};
			let res = resp;
			console.log("res.data.message", res.data && res.data.message);
			if (res.data && error_message[res.data.message]) {
				res.data.message = error_message[res.data.message];
			}
			if (resp.status == "1000") {
				resolve(res);
			} else {
				reject(res);
				if (resp.status == "3001") {
					// 权限相关
					let showMsg = "";
					if (platform() == "ios") {
						showMsg = `本应用暂时无法访问相机，请进入“设置-隐私-相机”进行授权。`;
					} else {
						showMsg = `暂时无法打开相机，请进入权限管理中授信本应用读取存储文件或照片。`;
					}
					alertTwoButt(showMsg, "去设置", "取消", () => {
						settingPrivacy();
					});
				}
			}
		});
	});
}

// ==============私有方法================
